package com.zn.task;

import com.zn.asycn.CalculateKLineExecutor;
import com.zn.socketReceiver.SocketHandler;
import com.zn.util.CSocketInfo;
import com.zn.web.rpc.MdRemoteCall;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


/**
 * Created by yinjiawei on 2018/7/12.
 * 监听socket是否正常的task
 */
@Component
public class ListenerSocketState {
    private static final Logger LOGGER = LoggerFactory.getLogger(ListenerSocketState.class);
//    @Value("${md.socket.host}")
//    private String host;
//    @Value("${md.socket.port}")
//    private String port;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private MdRemoteCall mdRemoteCall;
    @Autowired
    private CalculateKLineExecutor calculateKLineExecutor;
    @Autowired
    private CSocketInfo socketInfo;

    @Scheduled(cron="*/5 * * * * ?")   //每5秒执行一次
    public void task(){
        try{
            SocketHandler socketHandler=SocketHandler.getInstance(socketInfo.getMdHost(),socketInfo.getMdPort(),redisTemplate,mongoTemplate,calculateKLineExecutor,mdRemoteCall);
            boolean isClose=socketHandler.isServerClose();
            String isCloseStr=isClose==true?"关闭":"开启";
//            LOGGER.warn("MD....socket是否关闭:" + isCloseStr);
            //如果是关闭的 需要重连
            if(isClose){
                SocketHandler.setEmpty();
                SocketHandler.getInstance(socketInfo.getMdHost(),socketInfo.getMdPort(),redisTemplate,mongoTemplate,calculateKLineExecutor,mdRemoteCall);
            }else{
            }
        }catch (Exception e){
            LOGGER.error("MD...ListenerSocketState Error:" + e);
        }
    }

//    private void sendStatus(Integer socketStatus){
//        try {
//            String uniqueIdentification=redisTemplate.opsForValue().get(Constant.BUSINESSUNIQUE);
//            if(StringUtils.isNotBlank(uniqueIdentification)){
//                mdRemoteCall.sendSocketStatus(uniqueIdentification, 1, socketStatus);
//            }else{
//                LOGGER.error("uniqueIdentification is null:" + uniqueIdentification);
//            }
//        }catch (Exception e){
//            LOGGER.error("sendStatus......."+e);
//        }
//
//    }
//
//    @Scheduled(cron="*/1 * 09-11 * * ?")
//    public void getEtfMdInfo(){
//        try {
//            asyncTask.getEtfMdInfo();
//        }catch (Exception ex){
//            LOGGER.error("getEtfMdInfo:" + ex.getMessage());
//        }
//    }
//
//    @Scheduled(cron="*/1 * 13-14 * * ?")
//    public void getEtfMdInfo1(){
//        try {
//            asyncTask.getEtfMdInfo();
//        }catch (Exception ex){
//            LOGGER.error("getEtfMdInfo1:" + ex.getMessage());
//        }
//    }


}
